package com.xfan.core.web;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import com.xfan.core.util.JsonUtil;
public class MapBean<K, V> extends HashMap<K, V> {
	private static final long serialVersionUID = 5819387600888005124L;

	public MapBean() {
	}

	public MapBean(Object... args) {
		put(args);
	}

	public Integer getInt(Object key) {
		return (Integer) get(key);
	}

	public int getInt(Object key, int defaultInt) {
		Integer i = (Integer) get(key);
		return i == null ? defaultInt : i;
	}

	public String getString(Object key) {
		return (String) get(key);
	}

	public Long getLong(Object key) {
		return (Long) get(key);
	}

	public Long getLong(Object key, int defaultInt) {
		Long i = (Long) get(key);
		return i == null ? defaultInt : i;
	}

	public String getString(Object key, String defaultValue) {
		String value = (String) get(key);
		return value == null ? defaultValue : value;
	}

	public Timestamp getTimestamp(Object key) {
		return (Timestamp) get(key);
	}

	public void put(Object... args) {
		for (int i = 1; i < args.length; i += 2) {
			put(String.valueOf(args[i - 1]), args[i]);
		}
	}
	
	public String toJson(){
		return JsonUtil.getJson(this);
	}
	
	public String toJson(Object... keys){
		Map <Object,Object> tem=new HashMap<Object,Object>();
		for (int i = 0; i < keys.length; i++) {
			tem.put(keys[i], this.get(keys[i]));
		}
		return JsonUtil.getJson(tem);
	}
}
